package retrofitdemo.com.night.activity;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.lzy.okgo.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;
import retrofitdemo.com.night.R;
import retrofitdemo.com.night.adapter.CollectAdapter;
import retrofitdemo.com.night.adapter.SignAdapter;
import retrofitdemo.com.night.bean.CollectCallback;
import retrofitdemo.com.night.bean.PlayCarCallbackBean;
import retrofitdemo.com.night.databinding.ActivitySignInBinding;
import retrofitdemo.com.night.utils.GsonUtil;
import retrofitdemo.com.night.utils.HTTPAPI;
import retrofitdemo.com.night.utils.LogUtils;

public class SignInActivity extends BaseActivity {

    ActivitySignInBinding binding;
    String type;

    List<String> signs = new ArrayList<>();
    List<String> signsSecond = new ArrayList<>();
    SignAdapter signAdapter;

    List<CollectCallback.ResultBean> list = new ArrayList<>();
    List<CollectCallback.ResultBean> listSecond = new ArrayList<>();
    CollectAdapter collectAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_sign_in);
        binding.setActivity(this);
        initView();
    }

    private void initView() {

        setBack();
        type = getIntent().getStringExtra("module_type");
        switch (type){
            case "signIn":
                setTitleText("签到");

                signAdapter = new SignAdapter(SignInActivity.this,signs);
                binding.recycler.setLayoutManager(new LinearLayoutManager(this));
                binding.recycler.setItemAnimator(new DefaultItemAnimator());
                binding.recycler.setAdapter(signAdapter);

                playCarHistory();
                break;
            case "collect":
                setTitleText("收藏");

                collectAdapter = new CollectAdapter(SignInActivity.this,list);
                binding.recycler.setLayoutManager(new LinearLayoutManager(this));
                binding.recycler.setItemAnimator(new DefaultItemAnimator());
                binding.recycler.setAdapter(collectAdapter);


                collectHistory();
                break;
                default:
        }

    }

    public void playCarHistory(){
        HTTPAPI.getInstance().playCarHistory(SignInActivity.this,"2020-04", new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtils.e("打卡回显-->"+ s);
                PlayCarCallbackBean bean = GsonUtil.GsonToBean(s,PlayCarCallbackBean.class);
                if ("200".equals(bean.getCode())) {
                    signs.clear();
                    signsSecond = bean.getResult();
                    signs.addAll(signsSecond);
                    signAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    public void collectHistory(){
        HTTPAPI.getInstance().collectHistory(SignInActivity.this, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtils.e("收藏-->"+ s);
                CollectCallback bean = GsonUtil.GsonToBean(s, CollectCallback.class);
                if ("200".equals(bean.getCode())) {
                    list.clear();
                    listSecond = bean.getResult();
                    list.addAll(listSecond);
                    collectAdapter.notifyDataSetChanged();
                }
            }
        });
    }
}
